package com.wjy.myokhttp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.wjy.myokhttp.net.ApiService;
import com.wjy.myokhttp.net.NetSubscribe;
import com.wjy.myokhttp.net.RetrofitHelper;
import com.wjy.myokhttp.tools.Base64StringTool;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_get_url,btn_get,btn_post;
    private Button btn_get_map;
    private Button btn_post_map,btn_post_body;
    private Button btn_get_http,btn_post_http;
    private Button btn_uploadImage;

    Button btn_test;
    TextView tv_test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        btn_get_url = findViewById(R.id.btn_get_url);
        btn_get_url.setOnClickListener(this);
        btn_get = findViewById(R.id.btn_get);
        btn_get.setOnClickListener(this);
        btn_post = findViewById(R.id.btn_post);
        btn_post.setOnClickListener(this);

        btn_get_map = findViewById(R.id.btn_get_map);
        btn_get_map.setOnClickListener(this);

        btn_post_map = findViewById(R.id.btn_post_map);
        btn_post_map.setOnClickListener(this);
        btn_post_body = findViewById(R.id.btn_post_body);
        btn_post_body.setOnClickListener(this);

        btn_get_http = findViewById(R.id.btn_get_http);
        btn_get_http.setOnClickListener(this);
        btn_post_http = findViewById(R.id.btn_post_http);
        btn_post_http.setOnClickListener(this);

        btn_uploadImage = findViewById(R.id.btn_uploadImage);
        btn_uploadImage.setOnClickListener(this);

        btn_test = findViewById(R.id.btn_test);
        btn_test.setOnClickListener(this);
        tv_test = findViewById(R.id.tv_test);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_get_url:
                requestGet_Url();
                break;
            case R.id.btn_get:
                requestLogin_get();
                break;
            case R.id.btn_post:
                requestLogin_post();
                break;
            case R.id.btn_get_map:
                requestLogin_get_map();
                break;
            case R.id.btn_post_map:
                requestLogin_post_map();
                break;
            case R.id.btn_post_body:
                requestLogin_post_body();
                break;
            case R.id.btn_get_http:
                requestLogin_get_http();
                break;
            case R.id.btn_post_http:
                requestLogin_post_http();
                break;
            case R.id.btn_uploadImage:
                requestuploadImage();
                break;
            case R.id.btn_test:
                getJson();
                break;
        }
    }

    /**
     * get的Url请求
     */
    private void requestGet_Url(){
        RetrofitHelper.getInstance()
                .requestData("http://59.110.162.30/app_updater_version.json")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetSubscribe<JsonObject>(MainActivity.this) {
                    @Override
                    public void onNext(JsonObject value) {
                        Log.e("MainActivity","onNext  value="+value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("MainActivity","onError  e="+e);
                        e.printStackTrace();
                    }
                });
    }

    /**
     * get请求
     */
    private void requestLogin_get(){
        RetrofitHelper.getInstance()
                .requestLogin_get("13801234567","123456","100d8559092c7ef7d5a")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetSubscribe<JsonObject>(MainActivity.this) {
                    @Override
                    public void onNext(JsonObject value) {
                        Log.e("MainActivity","onNext  value="+value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("MainActivity","onError  e="+e);
                        e.printStackTrace();
                    }
                });
    }

    /**
     * post请求
     */
    private void requestLogin_post(){
        RetrofitHelper.getInstance()
                .requestLogin_post("13801234567","123456","100d8559092c7ef7d5a")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetSubscribe<JsonObject>(MainActivity.this) {
                    @Override
                    public void onNext(JsonObject value) {
                        Log.e("MainActivity","onNext  value="+value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("MainActivity","onError  e="+e);
                        e.printStackTrace();
                    }
                });
    }

    /**
     * get请求Map
     */
    private void requestLogin_get_map(){
        Map<String,String> params = new HashMap<>();
        params.put("username","13801234567");
        params.put("pwd","123456");
        params.put("regid","100d8559092c7ef7d5a");
        RetrofitHelper.getInstance()
                .requestLogin_get_map(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetSubscribe<JsonObject>(MainActivity.this) {
                    @Override
                    public void onNext(JsonObject value) {
                        Log.e("MainActivity","onNext  value="+value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("MainActivity","onError  e="+e);
                        e.printStackTrace();
                    }
                });
    }

    /**
     * post请求  Map
     */
    private void requestLogin_post_map(){
        Map<String,String> params = new HashMap<>();
        params.put("username","13801234567");
        params.put("pwd","123456");
        params.put("regid","100d8559092c7ef7d5a");
        RetrofitHelper.getInstance()
                .requestLogin_post_map(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetSubscribe<JsonObject>(MainActivity.this) {
                    @Override
                    public void onNext(JsonObject value) {
                        Log.e("MainActivity","onNext  value="+value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("MainActivity","onError  e="+e);
                        e.printStackTrace();
                    }
                });
    }

    /**
     * post请求  Body
     * @Body实际上是将类转换成json实体作为请求体来请求网络,目前没找到可以接受json格式的请求体作为参数的网络，因此这里这是从形式说明怎么使用，这是没有问题
     */
    private void requestLogin_post_body(){
        ApiService.LoginInfo loginInfo = new ApiService.LoginInfo();
        loginInfo.username = "13801234567";
        loginInfo.pwd = "123456";
        loginInfo.regid = "100d8559092c7ef7d5a";
        RetrofitHelper.getInstance()
                .requestLogin_post_body(loginInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetSubscribe<JsonObject>(MainActivity.this) {
                    @Override
                    public void onNext(JsonObject value) {
                        Log.e("MainActivity","onNext  value="+value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("MainActivity","onError  e="+e);
                        e.printStackTrace();
                    }
                });
    }

    /**
     * get请求  Http
     */
    private void requestLogin_get_http(){
        RetrofitHelper.getInstance()
                .requestLogin_get_http("13801234567","123456","100d8559092c7ef7d5a")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetSubscribe<JsonObject>(MainActivity.this) {
                    @Override
                    public void onNext(JsonObject value) {
                        Log.e("MainActivity","onNext  value="+value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("MainActivity","onError  e="+e);
                        e.printStackTrace();
                    }
                });
    }

    /**
     * post请求  Http
     */
    private void requestLogin_post_http(){
        RetrofitHelper.getInstance()
                .requestLogin_post_http("13801234567","123456","100d8559092c7ef7d5a")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetSubscribe<JsonObject>(MainActivity.this) {
                    @Override
                    public void onNext(JsonObject value) {
                        Log.e("MainActivity","onNext  value="+value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("MainActivity","onError  e="+e);
                        e.printStackTrace();
                    }
                });
    }

    /**
     * 图片上传
     */
    private void requestuploadImage(){
        String stream = null;
        String filePath = "/sdcard/UVCCamera/img_goods.png";//图片地址
        stream = Base64StringTool.getcomImageBase64(Base64StringTool.getSmallBitmap(filePath));
        if (TextUtils.isEmpty(stream)) {
            Toast.makeText(MainActivity.this,"图片上传失败！",Toast.LENGTH_SHORT).show();
            return;
        }
        RetrofitHelper.getInstance()
                .uploadImage("dev",stream)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NetSubscribe<JsonObject>(MainActivity.this) {
                    @Override
                    public void onNext(JsonObject value) {
                        Log.e("MainActivity","onNext  value="+value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("MainActivity","onError  e="+e);
                        e.printStackTrace();
                    }
                });
    }

    private void getJson(){
        String empty1 = null;
        //JsonObject需要添加gson jar包，通过com.google.gson.JsonObject来导入
        JsonObject object = new JsonObject();
        object.addProperty("name","张三");//String字符串
        object.addProperty("id",1);//int 数字
        object.addProperty("boolen",true);//boolen
        object.addProperty("empty1",empty1);//传空  null
        object.addProperty("empty2","");//传空  ""
        Log.e("getJson","object="+object.toString());//打印生成的json字符串

        //JSONObject是Android原生的json类，通过import org.json.JSONObject来导入
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name","张三");
            jsonObject.put("id",1);
            jsonObject.put("boolen",true);
            jsonObject.put("empty1",null);
            jsonObject.put("empty2","");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("getJson","jsonObject="+jsonObject.toString());

        //orj.json
        JSONArray jsonArray1 = new JSONArray();
        jsonArray1.put("admin");
        jsonArray1.put(true);
        jsonArray1.put(1);
        try {
            jsonArray1.put(0,"admin");
            jsonArray1.put(1,true);
            jsonArray1.put(2,1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonArray1.put(object);
        Log.e("getJson","jsonArray1="+jsonArray1);


        JSONObject jsonObject2 = new JSONObject();
        try {
            jsonObject2.put("name","李四");
            jsonObject2.put("id",2);
            jsonObject2.put("boolen",false);
            jsonObject2.put("empty3",null);
            jsonObject2.put("empty4","");
            jsonObject2.put("jsonArray",jsonArray1);//Android原生的json类里添加jsonArray
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("getJson","jsonObject2="+jsonObject2);

        //com.google.gson
        JsonArray jsonArray = new JsonArray();
        jsonArray.add("admin");
        jsonArray.add(true);
        jsonArray.add(1);

        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("name","李四");
            jsonObject1.put("id",2);
            jsonObject1.put("boolen",false);
            jsonObject1.put("empty3",null);
            jsonObject1.put("empty4","");
            jsonObject1.put("jsonArray",jsonArray);//Android原生的json类里添加jsonArray
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("getJson","jsonObject1="+jsonObject1);

        try {
            jsonObject.put("jsonArray",jsonArray);//Android原生的json类里添加jsonArray
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("getJson","jsonObject======"+jsonObject);
    }
}
